package com.haitaoit.chengquan.module.home.adapter.forum;

import android.app.Activity;
import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.home.model.ForumDetailEvaModel;
import com.haitaoit.chengquan.view.CircleImageView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/10/21.
 */
public class ForumDetailEvaAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<ForumDetailEvaModel> list;

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public ForumDetailEvaAdapter(Context context) {
        this.context = context;
    }

    public List<ForumDetailEvaModel> getList() {
        return list;
    }

    public void setList(List<ForumDetailEvaModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.forum_detail_eva_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ImageLoader.getInstance().displayImage(list.get(position).getHead_portrait(), holder.center_img);
        holder.center_img.setVisibility(View.GONE);
        holder.eva_name.setText(list.get(position).getName());
        holder.eva_time.setText(list.get(position).getAddtime());
//        holder.content.setText(list.get(position).getName() + "：" + list.get(position).getContent());
        holder.content.setText(getTextColor(list.get(position).getName(),list.get(position).getContent()));

        holder.addtime.setText("评论时间：" + list.get(position).getAddtime());
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.center_img)
        CircleImageView center_img;
        @ViewInject(R.id.eva_name)
        TextView eva_name;
        @ViewInject(R.id.eva_time)
        TextView eva_time;
        @ViewInject(R.id.content)
        TextView content;
        @ViewInject(R.id.addtime)
        TextView addtime;
    }
//    SpannableUtils
    private SpannableString getTextColor(String name,String content){

        SpannableString spannableString = new SpannableString(name+":"+content);
        spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.red)), 0, name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        String rep="回复";
        if(name.contains(rep)){
            int nameIndex=name.indexOf(rep);
            spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.forumd_blue)), nameIndex, nameIndex+rep.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

     return spannableString;
    }

}
